package com.minivision.sdktestdemo.model;

import com.minivision.sdktestdemo.bean.KeyBean;
import com.minivision.sdktestdemo.bean.MusicBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by devtang on 2016/4/11.
 */
public class AlbumProvider extends GroupProvider {
    private static AlbumProvider sAlbumProvider;

    private AlbumProvider(MusicProvider provider) {
        super(provider);
    }

    public static AlbumProvider getInstance(MusicProvider provider) {
        if (sAlbumProvider == null) {
            synchronized (AlbumProvider.class) {
                if (sAlbumProvider == null) {
                    sAlbumProvider = new AlbumProvider(provider);
                }
            }
        }
        return sAlbumProvider;
    }

    public static void release() {
        sAlbumProvider = null;
    }

    @Override
    public String getKey(MusicProvider provider, int position) {
        return provider.get(position).getmAlbumName();
    }

    public int insert(MusicBean bean) {
        int index = 0;
        for (KeyBean item : mList) {
            if (item.getKey().equals(bean.getmAlbumName())) {
                item.getmListSongs().add(bean);
                return index;
            }
            ++index;
        }
        if (index == mList.size()) {
            List<MusicBean> list = new ArrayList<>();
            list.add(bean);
            mList.add(new KeyBean(bean.getmAlbumName(), list, bean.getmPicUri()));
        }
        return index;
    }
}
